package Homework;

import java.util.Random;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class CallName {

	public static void main(String[] args) throws AgeException {
		ArrayList<Student> students = new ArrayList<Student>();
		addStudentName(students);
		printStudentName(students);
		String randName = randomStudentName(students);
		System.out.println("幸运儿是"+randName);
	}
	
	public static String randomStudentName(ArrayList<Student> students) {
		int index = new Random().nextInt(students.size());
		return students.get(index).getName();
	}

	public static void printStudentName(ArrayList<Student> students) {
		for(int i = 0;i<students.size();i++){
			System.out.println("第"+(i+1)+"个学生的名称是"+students.get(i).getName());
		}
	}

	public static void addStudentName(ArrayList<Student> students) throws AgeException{
		Scanner sc = new Scanner(System.in);
		Student s = new Student();
		System.out.println("请输入姓名：");
		s.setName(sc.next());
		System.out.println("请输入年龄：");
		boolean flag = true;
		while(flag){
			try {
				Scanner age = new Scanner(System.in);
				s.setAge(age.nextInt());
				flag=false;
			} catch (AgeException e) {
				System.out.println(e.toString());
				System.out.println("年龄必须位于18~25之间!\n请重新输入：");
			} catch (InputMismatchException e) {
				System.out.println("年龄必须为数字!\n请重新输入：");
			}
		}
		s.setId(students.size()+1);
		students.add(s);		
	}
}


